from Autotests.LocalMachine.Controls import WebButton, WebComboBox
from Autotests.LocalMachine.Controls.TableView import WebTableView
from Autotests.LocalMachine.Pages.BaseDialog import BaseWebDialog
from Autotests.LocalMachine.UiObjectsMap import AirManager


class DevicePage(BaseWebDialog):
    def __init__(self):
        super(DevicePage, self).__init__()
        self.goToHomeLocation()
        self._groupComboBox = WebComboBox(AirManager.Devices.groupComboBox)

    def goToHomeLocation(self):
        pass

    def cancel(self):
        WebButton(AirManager.Devices.btnClose).click()

    @BaseWebDialog.getter
    def assignGroup(self, groupName):
        self._groupComboBox.selectItemsByText(groupName)

    @BaseWebDialog.getter
    def unassignGroup(self):
        self._groupComboBox.selectItemsByDataValue('0')


class Overview(DevicePage):
    def __init__(self):
        super(Overview, self).__init__()
        self._tableEmailOs = WebTableView(self.waitChildControl(*AirManager.Devices.Overview.tableEmailOs))
        self._tableDataSources = WebTableView(self.waitChildControl(*AirManager.Devices.Overview.tableDataSources))

    def goToHomeLocation(self):
        WebButton(AirManager.Devices.btnOverview).click()

    def getEmailInfo(self):
        return self._tableEmailOs.item(0,1).text

    def getOSInfo(self):
        return self._tableEmailOs.item(1,1).text

    def getDataSourceInfo(self, rowNumber):
        return self._tableDataSources.getRowAsList(rowNumber)


class BackupInfo(DevicePage):
    def __init__(self):
        super(BackupInfo, self).__init__()
        self._tableBackupInfo = WebTableView(self.waitChildControl(*AirManager.Devices.Backup.tableBackupInfo))

    def goToHomeLocation(self):
        WebButton(AirManager.Devices.btnBackup).click()

    def getBackupInfo(self, rowNumber):
        return self._tableBackupInfo.getRowAsList(rowNumber)


class Statistics(DevicePage):
    def __init__(self):
        super(Statistics, self).__init__()
        self._tableGeneral = WebTableView(self.waitChildControl(*AirManager.Devices.Statistic.tableGeneral))
        self._tableAdvanced = WebTableView(self.waitChildControl(*AirManager.Devices.Statistic.tableAdvanced))
        self._tableFiles = WebTableView(self.waitChildControl(*AirManager.Devices.Statistic.tableFiles))

    def goToHomeLocation(self):
        WebButton(AirManager.Devices.btnStatistic).click()

    def _isTableHasEmptyField(self, table):
        for i in range(len(table.items())):
            if '' in table.getRowAsList(i):
                return False
        return True

    #TODO: Scrolling does not work.
    # It should help - DRIVER().execute_script("statisticElement.data('jsp').scrollByY(50)")
    def checkTableGeneralHasNoEmptyField(self):
        return self._isTableHasEmptyField(self._tableGeneral)

    def checkTableAdvancedHasNoEmptyField(self):
        return self._isTableHasEmptyField(self._tableAdvanced)

    def checkTableFilesHasNoEmptyField(self):
        return self._isTableHasEmptyField(self._tableFiles)

    def checkTablesIsNotEmpty(self):
        if  len(self._tableGeneral.items()) > 0 and \
            len(self._tableAdvanced.items()) > 0 and \
            len(self._tableFiles.items()) > 0:
            return True
        return False


class ErrorsInfo(DevicePage):
    def __init__(self):
        super(ErrorsInfo, self).__init__()
        self._tableErrorsInfo = WebTableView(self.waitChildControl(*AirManager.Devices.Errors.tableErrors))

    def goToHomeLocation(self):
        WebButton(AirManager.Devices.btnErrors).click()

    def getErrorsCount(self):
        return len(self._tableErrorsInfo.items())